<?php
/**
 * Created by 深圳市丹宏昊天电子商务有限公司.
 * 作 者: sqingwen
 * 日 期: 2016/12/15
 * 批量单列表页面
 */
?>
<link rel="stylesheet" href="<?= base_url() . 'resource/layui/css/layui.css' ?>" media="all">
<script type="text/javascript" src="<?= base_url() . 'resource/layui/layui.js' ?>" charset="utf-8"></script>
<script type="text/javascript" src="<?= base_url() . 'resource/js/jquery.PrintArea.js' ?>"></script>


<style>
    s {
        font-style: normal
    }

    .layui-input, .layui-textarea {
        width: 93%;
    }

    .btnSearch {
        line-height: 35px;
        height: 35px;
        padding: 0 15px;
    }

    .layui-table td, .layui-table th {
        padding: 9px 6px;
    }
    a {
    color: #0a8e61;
}
</style>
<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1><?=lang('menu_multi_index')?></h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list"></i><?=lang('menu_multi_index')?></h3>
            </div>
            <div class="panel-body">
                <div class="well" style="padding:0 19px;">
                  <div class="row">
                    <form action="" method="post">
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label class="control-label">选择商家</label>
                                <select name="customerId" id="customerId" class="form-control" data-edit-select="1">
                                    <option value="0" selected="selected"><?=lang('public_qxz')?></option>
                                    <?php foreach ($customerList as $k => $v): ?>
                                    <option value="<?= $v['id'] ?>"
                                    <?php if ($v['id'] == $_POST['customerId']) { ?>selected="selected"<?php } ?>><?= $v['name'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-4">
                        <div class="form-group">
                            <label class="control-label"><?= lang('l_55');?></label>
                            <input class="form-control" name="searchOrderNum" id="searchOrderNum" type="text" placeholder="<?= lang('l_55');//请输入维修单号?>"value="<?= isset($_POST['searchOrderNum']) ? $_POST['searchOrderNum'] : '' ?>">
                        </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group" style="padding-top: 35px;">
                                <input class="btn btn-primary" type="submit" class="layui-btn" value="<?= lang('l_60');//立即查询 ?>"/>
                            </div>
                        </div>
                        <script type="text/javascript">
                        $(function(){
                            $("select[data-edit-select]").filterSelect();
                        })
                    </script>
                    </form>
                  </div>
                </div>
                <div class="table-responsive">
                    <table class="layui-table" lay-skin="line">
                            <tbody>

                            <?php foreach ($list as $km => $vm): ?>
                                <tr>
                                    <td colspan="4"><?= $vm['merchantName'] ?>

                                    </td>
                                    <td colspan="3">
                                        <?= lang('l_78'); //最新订单时间                                         ?>：<em
                                                id="mt_<?= $vm['merchant_num'] ?>"></em>&nbsp;&nbsp;
                                        &nbsp;&nbsp;<?= lang('l_79_1'); //共 ?>
                                        <em
                                                id="mt_cnt_order_<?= $vm['merchant_num'] ?>">0</em><?= lang('l_79_2'); //个订单，到货进度：                                                                                                                 ?>
                                        <em
                                                id="mt_cnt_pro_<?= $vm['merchant_num'] ?>">0 / 0</em>
                                    </td>

                                    <td style="width: 20%;">
                                        <button class="layui-btn layui-btn-primary showOrdersByMer"
                                                dt-mernum="<?= $vm['merchant_num'] ?>" dt-status="0">
                                            <i class="layui-icon">&#xe60a;</i>
                                            <s><?= lang('l_86'); //展开订单                                                                                                                 ?></s>
                                        </button>


                                    </td>
                                </tr>

                                <tr class="mt_tr_head_<?= $vm['merchant_num'] ?>" style="display: none;">
                                    <td></td>
                                    <td>ID
                                        <button class="layui-btn layui-btn-primary merge_orders"
                                                dt-mernum="<?= $vm['merchant_num'] ?>">
                                            <i class="layui-icon">&#xe60a;</i>
                                            <s><?= lang('l_96'); //合并维修单                                                                                                                 ?></s>
                                        </button>
                                    </td>
                                    <td><?= lang('l_99'); //维修单号码                                                                                                                 ?></td>
                                    <td><?= lang('l_100'); //产品数量                                                                                                                 ?></td>
                                    <td><?= lang('l_101'); //来源站点                                                                                                                 ?></td>
                                    <td><?= lang('l_102'); //添加时间                                                                                                                 ?></td>
                                    <td><?= lang('l_103'); //到货百分比                                                                                                                 ?></td>
                                    <td><?= lang('l_104'); //操作                                                                                                                 ?></td>
                                </tr>

                                <!-- 商家订单 开始 -->
                            <?php foreach ($vm['orders'] as $key => $val): ?>
                                <tr class="mt_tr_<?= $vm['merchant_num'] ?>" dt_a_qty="<?= $val['actual_quantity'] ?>"
                                    dt_qty="<?= $val['quantity'] ?>"
                                    style="display: none;">
                                    <td></td>
                                    <td><?= $val['id'] ?>&nbsp;<input type="checkbox" value="<?= $val['order_num'] ?>"
                                                                      class="sub_cbk_<?= $vm['merchant_num'] ?>">
                                    </td>
                                    <td class="onum"><?= $val['order_num'] ?></td>
                                    <td><?= $val['quantity'] ?></td>
                                    <td><?= $val['point'] ?></td>
                                    <td class="order_add_time_<?= $vm['merchant_num'] ?>"><?= date('Y-m-d h:i:s' , $val['add_time']) ?></td>
                                    <td id="per_<?= $val['id'] ?>"
                                        style="<?= (($val['actual_quantity'] / $val['quantity']) > 1) ? 'color:#db3652;font-weight:bold;' : '' ?>">
                                        <?= number_format($val['actual_quantity'] / $val['quantity'] * 100 , 2) ?>%
                                    </td>
                                    <td>
                                        <button class="layui-btn layui-btn-primary shoow"
                                                data-id="<?= $val['id'] ?>"
                                                data-status="0" data-on="<?= $val['order_num'] ?>"
                                                data-qty="<?= $val['quantity'] ?>">
                                            <i class="layui-icon">&#xe60a;</i>
                                            <s><?= lang('l_128'); //展开来货单                                                                                                                 ?></s>
                                        </button>


                                        <button class="layui-btn layui-btn-primary btn_add_waybill"
                                                data-on="<?= $val['order_num'] ?>" data-ci="<?= $val['id'] ?>"
                                                data-qty="<?= $val['quantity'] ?>">
                                            <i class="layui-icon">&#xe608;</i>
                                            <s><?= lang('l_135'); //收货                                                                                                                 ?></s>
                                        </button>

                                        <button class="layui-btn layui-btn-primary btnOrderMore"
                                                dt-on="<?= $val['order_num'] ?>" dt-status="0">
                                            <i class="layui-icon">&#xe60a;</i>
                                            <s>More</s>
                                        </button>

                                        <button class="layui-btn layui-btn-primary"
                                                onclick="window.location.href='/sc/orders/Multi/DoExportOrders?orderNum=<?= $val['order_num'] ?>'">
                                            <i class="layui-icon">&#xe60a;</i>
                                            <s>Export</s>
                                        </button>
                                    </td>
                                </tr>
                                <tr id="ship_<?= $val['id'] ?>" class="tb_ship_id_<?= $vm['merchant_num'] ?>"
                                    style="display:none;">
                                    <td colspan="2">&nbsp;</td>
                                    <td colspan="6">

                                        <table class="table" id="waybill_tb_<?= $val['id'] ?>" style="width:99%;">
                                            <thead>
                                            <tr>
                                                <th><?= lang('l_148'); //拆包号                                                                                                                 ?></th>
                                                <th><?= lang('l_149'); //PID                                                                                                                 ?></th>
                                                <th><?= lang('l_150'); //来货单号                                                                                                                 ?></th>
                                                <th><?= lang('l_151'); //到货数量                                                                                                                 ?></th>
                                                <th><?= lang('l_152'); //到货时间                                                                                                                 ?></th>
                                                <th style="display: none"><?= lang('l_153'); //机器识别码                                                                                                                 ?></th>
                                                <th><?= lang('l_154'); //到货百分比                                                                                                                 ?></th>
                                                <th><?= lang('l_155'); //操作                                                                                                                 ?></th>
                                            </tr>
                                            </thead>
                                            <tbody id="tb_waybills_<?= $val['id'] ?>">

                                            </tbody>
                                        </table>

                                    </td>
                                </tr>
                            <?php endforeach; ?>
                                <!-- 商家订单 结束 -->
                                <script type="text/javascript">
                                    //最新订单时间
                                    $("#mt_<?= $vm['merchant_num'] ?>").text($(".order_add_time_<?= $vm['merchant_num'] ?>:last").text());
                                    //订单数量统计
                                    $("#mt_cnt_order_<?= $vm['merchant_num'] ?>").text($(".mt_tr_<?= $vm['merchant_num'] ?>").length);
                                    //到货数量统计
                                    var cnt_pro = 0;
                                    var cnt_qtty = 0;
                                    $('.mt_tr_<?= $vm['merchant_num'] ?>').each(function () {
                                        cnt_pro = cnt_pro + parseInt($(this).attr('dt_a_qty'));
                                        cnt_qtty = cnt_qtty + parseInt($(this).attr('dt_qty'))
                                        $("#mt_cnt_pro_<?= $vm['merchant_num'] ?>").text(cnt_pro + " / " + cnt_qtty);
                                    });
                                </script>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php if($show_pages):?>
                    <div class="page">
                        <?=$show_pages?>
                    </div>
                    <?php endif;?>
                </div>
            </div>
        </div>
    </div>

<!-- 以下是各个弹出层的效果 begin ********************************************************************************************************* -->
<!-- 遮罩层 begin -->
<div id="overlay"></div>
<!-- 遮罩层 end -->

<!-- 添加货运单 begin -->
<div class="add_wayboll">
    <div style="width:100%;text-align: right;">
        <a href="javascript:void(0);" class="add_wayboll_close"><i class="layui-icon"
                                                                   style="font-size: 30px; color: #1E9FFF;color:#db3652;">
                &#x1006;</i></a></div>
    <fieldset class="layui-elem-field">
        <legend><?= lang('l_215'); //添加来货单                                                                                                                 ?></legend>
        <div class="layui-field-box">
            <?= lang('l_217'); //维修单号                                         ?>：<span id="showOrderNum"></span>
        </div>
    </fieldset>
    <!-- Table -->
    <form class="form_waybill" style="max-height:250px;overflow-y:scroll;">
        <table class="layui-table" lay-skin="line" style="overfllow:hidden;">
            <tr style="display:none;">
                <td>
                    <?= lang('l_225'); //来货单号                                          ?>
                </td>
                <td>
                    <?= lang('l_228'); //到货数量                                         ?>
                </td>
                <td>
                    <?= lang('l_231'); //机器识别码                                         ?>
                </td>
            </tr>
            <tr id="way_af_tr">
                <td colspan="3">
                    <input type="text" class="layui-input"
                           placeholder="<?= lang('l_236'); //来货单号                                        ?>"
                           id="txtShipNum"
                           name="txtShipNum" style="height:25px;line-height:25px;">
                </td>
            </tr>


            <tr style="display:none;">
                <td colspan="3">
                    <textarea class="" id="txtRemark" name="txtRemark"
                              placeholder="<?= lang('l_251'); //备注                                ?>"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="text-align: right;">
                    <input type="hidden" value="0" id="hidOrderNum" name="hidOrderNum"/><!-- 维修单号 -->
                    <input type="hidden" value="0" id="hidContoinerId" name="hidContoinerId"/><!-- 维修单id -->
                    <input type="hidden" value="0" id="hidOrderQty" name="hidOrderQty"/><!-- 维修单预计数量 -->
                    <button class="btn btn-default btn-xs btnSubmittt" type="button">
                        <?= lang('l_260'); //打印条码                     ?>
                    </button>
                </td>
            </tr>
        </table>
    </form>
</div>
<!-- 添加货运单 end -->

<!-- 拆分包裹 begin -->
<div class="main_unpk" id="main_unpk">
    <fieldset class="layui-elem-field" style="width: 90%;margin: 0 auto;">
        <legend><?= lang('l_272'); //来货单拆分                                         ?></legend>
        <div class="layui-field-box">
            <?= lang('l_274_1'); //拆分                       ?>：<input type="text" name="btnUnpkQty" id="btnUnpkQty"
                                                                      placeholder="<?= lang('l_274_2'); //个数                                        ?>"
                                                                      onkeyup="this.value = this.value.replace(/\D/g, '')"
                                                                      onafterpaste="this.value=this.value.replace(/\D/g,'')"
                                                                      value="25"
                                                                      style="width: 60px;height: 24px;">
            <button type="button" name="btnUnpkSave" id="btnUnpkSave"
                    style="padding: 3px 8px;margin-left:20px;"><?= lang('l_278'); //拆包                                        ?>
            </button>
            <br/> <?= lang('l_280_1'); //剩余                                          ?>：<input type="text"
                                                                                               name="btnUnpkReQty"
                                                                                               id="btnUnpkReQty"
                                                                                               placeholder="<?= lang('l_280_2'); //剩余个数                                                                                                                 ?>"
                                                                                               value="0"
                                                                                               style="width: 60px;height: 24px;"
                                                                                               disabled="disabled">
            <button type="button" name="btnUnpkReset" id="btnUnpkReset"
                    style="padding: 3px 8px;margin-left:20px;"><?= lang('l_282'); //取消                                        ?>
            </button>
            <input type="hidden" name="hidPkQty" id="hidPkQty" value="0">
            <input type="hidden" name="hidPkId" id="hidPkId" value="0"><!-- 拆包单ID -->
            <input type="hidden" name="hidPkSort" id="hidPkSort" value="0"><!-- 拆包单排序 -->
            <input type="hidden" name="hidOnum" id="hidOnum" value="0"><!-- 维修单号 -->
            <input type="hidden" name="hidwNum" id="hidwNum" value="0"><!-- 来货单单号 -->
            <input type="hidden" name="hidGoid" id="hidGoid" value="0">
            <input type="hidden" name="hidGqty" id="hidGqty" value="0">
            <input type="hidden" name="hidUnpkum" id="hidUnpkum" value="0">
        </div>
    </fieldset>

</div>
<!-- 拆分包裹 end -->

<!-- 在列表里面的弹出的合并包裹弹出层 begin -->
<div class="main_merge" id="main_merge">
    <fieldset class="layui-elem-field" style="width: 90%;margin: 0 auto;">
        <legend><?= lang('l_301'); //合并包裹                                                                                                                 ?></legend>
        <div class="layui-field-box">
            <p>
                <label style="letter-spacing: 9px;font-weight: normal;"><?= lang('l_302'); //合并                                                                                                                 ?></label>：<input
                        type="text" name="btnMergeQty"
                        id="btnMergeQty"
                        placeholder="<?= lang('l_305'); //个数                                                                                                                 ?>"
                        disabled="disabled" value="25"
                        style="width: 50px;height: 24px;">
                <label style="letter-spacing: 9px;font-weight: normal;"><?= lang('l_308'); //剩余                                                                                                                 ?></label>：<input
                        type="text"
                        name="btnMergeReQty"
                        id="btnMergeReQty"
                        placeholder="<?= lang('l_311'); //剩余个数                                                                                                                 ?>"
                        value="0"
                        disabled="disabled"
                        style="width: 50px;height: 24px;">
            </p>
            <p style=" margin: 10px 0px;">
                <?= lang('l_317_1'); //包裹号                                         ?>：<input type="text"
                                                                                             name="txtMergeNum"
                                                                                             id="txtMergeNum"
                                                                                             placeholder="<?= lang('l_317_2'); //合并包裹号                                                                                                                 ?>"
                                                                                             value=""
                                                                                             style="width: 160px;height: 24px;"
                                                                                             disabled="disabled">
            </p>

            <p style=" margin: 10px 0px; display:none;">
                <?= lang('l_321'); //维修师                                         ?>：<select name="selectUser"
                                                                                            id="selectUser"
                                                                                            style="height: 27px;width: 164px;">
                    <option value="0"><?= lang('l_22'); //预设维修师                                                                                                                 ?></option>
                    <?php foreach ($users as $k => $v): ?>
                        <option value="<?= $v['id'] ?>"><?= ($v['chart']) ?>  <?= $v['name'] ?></option>
                    <?php endforeach; ?>
                </select>
            </p>

            <p>
                <button type="button" name="btnMergeSave" id="btnMergeSave" style="padding: 3px 8px;margin-left:20px;">
                    <?= lang('l_331'); // 合并                                         ?>
                </button>
                <input type="hidden" name="hidMMQty" id="hidMMQty" value="0">
                <input type="hidden" name="hidMMId" id="hidMMId" value="0">
                <input type="hidden" name="hidMMNum" id="hidMMNum" value="0">
                <button type="button" name="btnMergeReset" id="btnMergeReset"
                        style="padding: 3px 8px;margin-left:20px;"><?= lang('l_337'); //取消                                                                                                                 ?>
                </button>
            </p>
        </div>
    </fieldset>

</div>
<!-- 在列表里面的弹出的合并包裹弹出层 end -->

<!-- 查看订单请求的弹出层 begin -->
<div style="width:800px;height:450px;border:1px solid #e3e3e3;" id="showOrderDetails" class="easyui-dialog"
     closed="true" title="Order Detail" shadow="false">
    <div style="padding:10px;">
        <label style="font-weight:normal" id="dlgOrderAddr"></label>
    </div>
    <table id="showOrderDetailsTable" class="easyui-table" style="margin-top:20px;">

    </table>
</div>
<script type="text/javascript">
    $('.btnOrderMore').click(function () {
        $('#showOrderDetailsTable').datagrid({
            url: '<?= blang_url('orders/Multi/ajaxLoadProductsByOrderNum') ?>',
            queryParams: {'on': $(this).attr('dt-on')},
            height: 350,
            striped: true,
            fitColumns: true,
            singleSelect: false,
            rownumbers: true,
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 50],
            columns: [[
                {field: 'id', title: 'Pid'},
                {field: 'cat_name', title: 'Category'},
                {field: 'sku', title: 'Sku'},
                {field: 'imei', title: 'Imei'},
                {field: 'product_brand', title: 'Brand'},
                {field: 'product_model', title: 'Model'},
                {field: 'customerRemark', title: 'Remrak'},
                {field: 'defectDes', title: 'defectDes'}

            ]]
        });
        $('#showOrderDetails').panel({title: '[' + $(this).attr('dt-on') + '] Detail'});
        $("#showOrderDetails").panel("move", {top: $(document).scrollTop() + ($(window).height() - 450) * 0.5});
        $('#showOrderDetails').dialog('open');
        $.post('<?= blang_url('orders/Multi/ajaxLoadOrderAddress') ?>', {'on': $(this).attr('dt-on')}, function (data) {
            $('#dlgOrderAddr').html("Delivery Address:" + data.addr + "<br>Customer Info:" + data.customer);
        }, "json");
    });</script>
<!-- 查看订单请求的弹出层 end -->


<!-- 以上是各个弹出层的效果 end** ********************************************************************************************************* -->
<!-- 打印模块 begin -->
<style type="text/css">
    #myPrintArea {
        display: none;
    }
</style>
<div id="myPrintArea" style="overflow:hidden;margin:0 auto;margin-top:0;">
</div>

<!-- 打印模块  end -->
<script>
    //Demo
    layui.use('form', function () {
        var form = layui.form();
    });
    layui.use('element', function () {
        var element = layui.element();
    });</script>
<style type="text/css">

    #page {
        padding: 7px 0;
    }

    #page a {

    }

    #page a .current {

    }

    .add_wayboll {
        display: none;
        width: 700px;
        height: 400px;
        overflow: hidden;
        background: #fff;
        padding: 13px 21px;
        box-shadow: 0 0 3px #000;
        position: fixed;
        top: 50%;
        left: 50%;
        margin: -201px 0 0 -351px;
        z-index: 110;
    }

    /*合并包裹 begin*/
    .merge_package_div {
        display: none;
        width: 700px;
        background: #fff;
        padding: 13px 21px;
        box-shadow: 0 0 3px #000;
        position: fixed;
        top: 50%;
        left: 50%;
        margin: -201px 0 0 -351px;
        z-index: 110;
        height: 450px;
        overflow: hidden;
    }

    #table_merge_package td, #table_merge_package th {
        padding: 5px 15px;
    }

    input[type=checkbox] {
        vertical-align: middle;
    }

    .cbkWaybill {
        display: none;
    }

    .layui-tab {
        margin: -16px 0;
    }

    #main_merge {
        width: 300px;
        height: 200px;
        position: absolute;
        border: 1px solid #666;
        background: #fff;
        display: none;
        border-radius: 2px;
        box-shadow: 2px 2px 1px #888;
    }

    /*合并包裹 end*/

    /*拆分包裹 begin*/
    .main_unpk {
        width: 246px;
        height: 120px;
        position: absolute;
        border: 1px solid #666;
        background: #fff;
        display: none;
        border-radius: 2px;
        box-shadow: 2px 2px 1px #888;
    }

    /*拆分包裹 end*/
    /* 半透明的遮罩层 */
    #overlay {
        background: #000;
        filter: alpha(opacity=50); /* IE的透明度 */
        opacity: 0.5; /* 透明度 */
        display: none;
        position: absolute;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 100; /* 此处的图层要大于页面 */
        display: none;
    }

    .layui-tab-content {
        border: 1px solid #e2e2e2;
        border-top: none;
    }
</style>
<script type="text/javascript">
    $('#txtShipNum').keyup(function () {
        if ($.trim($('#txtShipNum').val()).length > 5) {
            $.ajax({
                type: "POST",
                async: false,
                url: '<?= blang_url('orders/Multi/ajaxCheckNum') ?>',
                data: {'nt': 'waybillNum', 'n': $.trim($('#txtShipNum').val())},
                dataType: 'json',
                success: function (data) {
                    if (data.cnt == 0) {
                        layer.tips('<?= lang('l_472'); //该单号可以使用！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
                        $('.btnSubmittt').removeAttr("disabled");
                    } else {
                        $('#txtShipNum').val($('#txtShipNum').val() + '_1')
                        //layer.tips('<?= lang('l_475'); //该单号已经存在！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
                        //$('.btnSubmittt').attr("disabled", true);
                    }
                }
            });
        } else {
            layer.tips('<?= lang('l_481'); //来货单号格式不正确！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
        }
    });
    $('.btnSubmittt').click(function () {
        var cid = $.trim($('#hidContoinerId').val()); //容器id
        var oqty = $.trim($('#hidOrderQty').val()); //订单总数
        var uri = '<?= blang_url('orders/Multi/ajaxAddWaybill') ?>';
        var num = $('#txtShipNum').val(); //来货单号
        var qty = $.trim($('#txtShipQuantity').val()); //到货数量
        var imeis = $.trim($('#txtShipImeiNums').val()); //机器识别码
        var remark = $.trim($('#txtRemark').val()); //机器识别码
        var orderNum = $.trim($('#hidOrderNum').val()); //维修单号


        if (num == '') {
            layer.tips('<?= lang('l_498'); //来货单号格式不正确！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
            $('#txtShipNum').focus();
            return;
        }
        //以下验证先行去掉
        /*if (qty == '' || qty == 0) {
         layer.tips('<?= lang('l_503'); //亲，没有到货数量哦！                                                                        ?>', '#txtShipQuantity', {tips: [3, '#008cd7']});
         $('#txtShipQuantity').focus();
         return;
         }*/
        if ($.trim($('#txtShipNum').val()).length <= 5) {
            layer.tips('<?= lang('l_508'); //来货单号格式不正确！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
            return;
        } else {

            //开始 解析sku的数量以及imei

            var qqty = '';
            var iimei = '';
            var ssku = '';
            $('.cl_ship_quantity').each(function (ii) {
                if ($(this).val() > 0) {
                    if (qqty != '') {
                        qqty += ',';
                    }
                    qqty += $(this).val();

                    if (ssku != '') {
                        ssku += ',';
                    }
                    ssku += $(this).attr('dt-sku');

                    if (iimei != '') {
                        iimei += '^';
                    }
                    iimei += $('.cl_imeis').eq(ii).val().replace(/[\r\n]/g, ",") == '' ? 'null' : $('.cl_imeis').eq(ii).val().replace(/[\r\n]/g, ",");
                    // qqty +=  $(this).attr('dt-sku') + '{' + $(this).val() + ':[' + $('.cl_imeis').eq(ii).val().replace(/[\r\n]/g, ",") + ']}';
                }

            });


            $.ajax({
                type: "POST",
                async: false,
                url: '<?= blang_url('orders/Multi/ajaxCheckNum') ?>',
                data: {'nt': 'waybillNum', 'n': num},
                dataType: 'json',
                success: function (data) {
                    if (data.cnt == 0) {
                        $.ajax({
                            type: "POST",
                            url: uri,
                            async: false,
                            data: {
                                'num': num,
                                'qty': qqty,
                                'sku': ssku,
                                'imeis': iimei,
                                'orderNum': orderNum,
                                'remark': remark
                            },
                            dataType: "text",
                            success: function (data) {
                                layer.msg("<?= lang('l_533'); //添加成功！                                                                        ?>");
                                //打印产品标签 begin

                                printWaybill(num, orderNum);
                                //打印产品标签 end
                                hideOverlay(); //关闭遮罩层
                                $('.add_wayboll').hide(); //关闭弹出层
                                clearAddDiv(); //清理输入控件残余值
                                loadWaybill(orderNum, cid, oqty); //重新ajax加载来货单
                                $('#per_' + cid).text();
                            }
                        });
                    } else {
                        layer.tips('<?= lang('l_543'); //该单号已经存在！                                                                        ?>', '#txtShipNum', {tips: [3, '#008cd7']});
                        return;
                    }
                }
            });
        }

    })
    ;
    $('.shoow').click(function () {
        var id = $(this).attr('data-id');
        var status = $(this).attr('data-status');
        var orderNum = $(this).attr('data-on'); //订单编号
        var qty = $(this).attr('data-qty'); //预计订单总量
        if (status == 0) {
            $('#ship_' + id).show();
            $(this).attr('data-status', '1');
            $(this).find('s').text('<?= lang('l_561'); //收起来货单                                                                        ?>');
            loadWaybill(orderNum, id, qty); //ajax加载来货单
        } else {
            $('#ship_' + id).hide();
            $(this).attr('data-status', '0');
            $(this).find('s').text('<?= lang('l_566'); //展开来货单                                                                        ?>');
        }

    });
    $('.show_code').click(function () {

    });
    //以下是添加货运单的js代码
    $('.btn_add_waybill').click(function () {
        var orderNum = $.trim($(this).attr('data-on'));
        var cid = $.trim($(this).attr('data-ci'));
        var oqty = $(this).attr('data-qty'); //订单总量

        var persent = $.trim($('#per_' + cid).text());
        persent = persent.replace('%', '');
        /*if (persent >= 100) {
           layer.msg('<?= lang('l_586'); //已经满了                                                                        ?>');
            return;
        }*/
        showOverlay(); //打开遮罩层
        $('.add_wayboll').show();
        //动态加载sku
        $.post('<?= blang_url('orders/Multi/ajaxGetWaybill') ?>', {'on': orderNum}, function (data) {
            $('.tttr_ttr').remove();
            $('#way_af_tr').after(data);
        }, 'text');
        //动态加载sku
        $('#showOrderNum').text(orderNum);
        $('#hidOrderNum').val(orderNum);
        $('#hidContoinerId').val(cid);
        $('#hidOrderQty').val(oqty);
    });
    $('.add_wayboll_close').click(function () {
        hideOverlay(); //关闭遮罩层
        $('.add_wayboll').hide();
        clearAddDiv(); //清理输入控件残余值

    });
    clearAddDiv = function () {
        $('#txtShipNum').val('');
        $('#txtShipQuantity').val('');
        $('#txtShipImeiNums').val('');
        $('#txtRemark').val('');
    }
    //以上是添加货运单的js代码


    //展开的时候显示维修单号所属的来货单列表 begin
    loadWaybill = function (orNum, id, qty) {
        var uri = '<?= blang_url('orders/Multi/ajaxGetWaybillByOrderNum') ?>';
        var lload;
        $.ajax({
            type: "POST",
            url: uri,
            async: false,
            data: {
                'orderNum': orNum
            },
            beforeSend: function (XMLHttpRequest) {
                lload = layer.load(1, {shade: [0.1, '#fff']});
            },
            complete: function (XMLHttpRequest, textStatus) {

                layer.close(lload);
            },
            dataType: "json",
            success: function (data) {
                if (data.length > 0) {
                    var list;
                    var subtotalqty = 0;
                    for (var i = 0; i < data.length; i++) {
                        subtotalqty = subtotalqty + parseInt(data[i].quantity); //累加数量
                        if (data[i].mp_num != '' && data[i].mp_num != null) {
                            sele = "display:none"
                        } else {
                            sele = "";
                        }
                        list += '<tr><td>' + data[i].unpackage_num + '</td><td><a href="javascript:void(0);" onclick=\'layer.alert("' + data[i].product_ids + '")\'><?= lang('l_645_1'); //查看PID                                                                        ?></a></td><td>' + data[i].waybill_num + '</td><td>' + data[i].quantity + '</td><td>' + data[i].add_time + '</td><td style="display:none;"><a href="javascript:void(0)"><?= lang('l_645_2'); //查看识别码                                                                        ?></a> </td><td class="subtoteltd">' + ((subtotalqty / qty) * 100).toFixed(2) + '%</td><td><button class="layui-btn layui-btn-primary btnUnpk" style="' + sele + '" dt-on="' + data[i].order_num + '" dt-wn="' + data[i].waybill_num + '" dt-unid="' + data[i].mmuid + '" dt-unsort="' + data[i].unpk_cnt + '" dt-qty="' + data[i].quantity + '" dt-gqty="' + qty + '" dt-goid="' + id + '" dt-unpknum="' + data[i].unpackage_num + '"><i class="layui-icon">&#xe61d;</i> <s><?= lang('l_645_3'); //拆分包裹                                                                        ?></s></button><span class="layui-btn layui-btn-primary chkCheck" style="color: #333; ' + sele + '"><i class="layui-icon" style="margin-left:-10px;">&#xe618;</i> <s><?= lang('l_645_4'); //选择                                                                        ?></s> <input type="checkbox" class="cbkWaybill" name="cbkWaybill" value="' + data[i].quantity + '" dt-uid="' + data[i].mmuid + '" dt-tid="' + id + '" dt-oqty="' + qty + '" dt-unnum="' + data[i].unpackage_num + '" dt-ordernum="' + data[i].order_num + '"></span>   <button class="layui-btn layui-btn-primary" onclick="printWaybill(\'' + data[i].waybill_num + '\',\'' + data[i].order_num + '\')" >PRINT</button></td><tr>';
                    }
                    $('#tb_waybills_' + id).html(list);
                    setQtyAndAdd(id);
                    //重新绑定事件 begin
                    $(document).on("click", '.btnUnpk', function () {
                        //打开拆分的操作 begin
                        $('#main_unpk').show();
                        $('#btnMergeReset').trigger('click');
                        // $('#main_merge').hide();
                        //弹出层定位
                        $('#main_unpk').css('top', $(this).position().top);
                        $('#main_unpk').css('left', $(this).position().left - 250);
                        //设置数量
                        $("#hidPkQty").val($(this).attr('dt-qty'));
                        $('#btnUnpkQty').val($(this).attr('dt-qty'));
                        $('#hidPkId').val($(this).attr('dt-unid'));
                        $('#hidPkSort').val($(this).attr('dt-unsort'));
                        $('#hidOnum').val($(this).attr('dt-on'));
                        $('#hidUnpkum').val($(this).attr('dt-unpknum'));
                        $('#hidwNum').val($(this).attr('dt-wn'));
                        $('#hidGqty').val($(this).attr('dt-gqty'));
                        $('#hidGoid').val($(this).attr('dt-goid'));
                        $('#btnUnpkReQty').val(0);
                        //打开拆分的操作 end
                    });
                    //重新绑定事件 end
                    cbkcheckkk();
                } else {
                    $('#tb_waybills_' + id).html('<tr><td colspan="8"><strong><?= lang('l_678'); //抱歉，暂时没有来货单！                                                                        ?></strong></td></tr>');
                }
                layer.close(lload);
            }
        });
    }
    //展开的时候显示物流单号的产品列表 end

    //弹出合并包裹弹出层 begin
    $('.btn_merge_package').click(function () {
        var orderNum = $.trim($(this).attr('data-on'));
        $('#hidOrderQty').val($(this).attr('data-qty'));
        showOverlay();
        $('.merge_package_div').show();
        loadMegePackage(orderNum);
    });
    //弹出合并包裹弹出层 end


    //关闭合并包裹 begin
    $('.add_wayboll_close').click(function () {
        hideOverlay(); //关闭遮罩层
        $('.merge_package_div').hide();
    });
    //关闭合并包裹 end

    //加载合并包裹 begin
    loadMegePackage = function (orderNum) {
        var uri = '<?= blang_url('orders/Multi/ajaxGetWaybillByOrderNum') ?>';
        var lload;
        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'orderNum': orderNum
            },
            beforeSend: function (XMLHttpRequest) {
                lload = layer.load(1, {shade: [0.1, '#fff']});
            },
            complete: function (XMLHttpRequest, textStatus) {
                layer.close(lload);
            },
            dataType: "json",
            success: function (data) {
                if (data.length > 0) {
                    var list;
                    var subtotalqty = 0;
                    for (var i = 0; i < data.length; i++) {

                        list += '<tr><td>' + data[i].waybill_num + '</td><td>' + data[i].quantity + '</td><td>' + data[i].add_time + '</td><td><button class="layui-btn layui-btn-primary btnUnpacking"><i class="layui-icon">&#xe61d;</i> <s><?= lang('l_730'); //拆分包裹                                                                        ?></s></button></td><tr>';
                    }
                    $('#tb_merge_package').html(list);
                } else {
                    $('#tb_merge_package').html('<tr><td colspan="4"><strong><?= lang('l_735'); //抱歉，暂时没有来货单！                                                                        ?></strong></td></tr>');
                }
                layer.close(lload);
            }
        });
    }

    //保存合并包裹 begin
    $('#btnMergeSave').click(function () {
        var cnts = ''; //计数器
        var nums = '';
        var tti = 0;
        var ms_order_num = '';
        $('.cbkWaybill').each(function () {
            if ($(this).is(':checked')) {
                sp = ',';
                if (tti == 0) {
                    sp = '';
                    tti++;
                }
                cnts = cnts + sp + $(this).attr('dt-uid');
                nums = nums + sp + $(this).attr('dt-unnum');
            }
            ms_order_num = $(this).attr('dt-ordernum');
        });
        if (parseInt($('#btnMergeQty').val()) == 0) {
            layer.msg('<?= lang('l_763'); //亲，您没有选择合并的包裹哦                                                                        ?>');
            return;
        }
        var uri = '<?= blang_url('orders/Multi/ajaxMergePackage') ?>';
        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'num': $('#txtMergeNum').val(),
                'order_num': $('#hidMMNum').val(),
                'unpk_ids': cnts,
                'userid': $('#selectUser').val(),
                'unpk_nums': nums
            },
            dataType: "text",
            beforeSend: function () {
                $('#btnMergeSave').attr("disabled", "disabled");
                $('#btnMergeSave').text("<?= lang('l_782'); //合并中...                                                                        ?>");
            },
            success: function (data) {
                layer.msg('<?= lang('l_785'); //合并成功                                                                        ?>');
                $('#btnMergeSave').removeAttr("disabled");
                $('#btnMergeSave').text("<?= lang('l_787'); //合并                                                                        ?>");
                $('#main_merge').hide(); //关闭弹出层
                loadWaybill($('#hidMMNum').val(), $('#hidMMId').val(), $('#hidMMQty').val()); //ajax加载来货单

            }
        });
    });
    //保存合并包裹 end

    //关闭合并包裹 begin
    $('#btnMergeReset').click(function () {
        $('#main_merge').hide();
        $('#txtMergeNum').val('');
        //如果已经被选中
        if ($('.chkCheck').find("input[type='checkbox']").is(':checked')) {
            //如果还没有没选中
            $('.chkCheck').css('background-color', '#fff');
            $('.chkCheck').css('color', '#333');
            $('.chkCheck').find("input[type='checkbox']").prop('checked', false);
        }
    });
    //关闭合并包裹 end


    //加载合并包裹 end

    //拆包 begin
    //拆包按钮事件 begin
    $('#btnUnpkSave').click(function () {
        if (parseInt($.trim($('#btnUnpkReQty').val())) == 0 || parseInt($.trim($('#btnUnpkQty').val())) == 0) {
            layer.msg("<?= lang('l_821'); //亲，不带这样玩的！0还拆分啥啊？                                                                        ?>");
            return;
        }
        var uri = '<?= blang_url('orders/Multi/ajaxAddUnpackage') ?>';
        var lload;
        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'orderNum': $('#hidOnum').val(),
                'uid': $('#hidPkId').val(),
                'num': $('#hidwNum').val(),
                'uncnt': $('#btnUnpkQty').val(),
                'recnt': $('#btnUnpkReQty').val(),
                'sort': $('#hidPkSort').val(),
                'unpkNum': $('#hidUnpkum').val()
            },
            dataType: "text",
            success: function (data) {
                layer.msg('<?= lang('l_841'); //拆包成功                                                                        ?>');
                closeUnpkDiv(); //关闭按钮
                loadWaybill($('#hidOnum').val(), $('#hidGoid').val(), $('#hidGqty').val()); //ajax加载来货单
            }
        });
    });
    //拆包按钮事件 end

    //关闭拆包
    $('#btnUnpkReset').click(function () {
        closeUnpkDiv();
    });
    $('#btnUnpkQty').keyup(function () {
        var qqty = parseInt($('#hidPkQty').val());
        if ($.trim($('#btnUnpkQty').val()) == '') {
            $('#btnUnpkQty').val(0);
            $('#btnUnpkReQty').val(qqty);
            return;
        }
        if (parseInt($('#btnUnpkQty').val()) > qqty) {
            layer.msg('<?= lang('l_861'); //超标了，亲                                                                        ?>');
            $('#btnUnpkQty').val(qqty);
            $('#btnUnpkReQty').val(0);
        } else {
            $('#btnUnpkReQty').val(qqty - parseInt($('#btnUnpkQty').val()));
        }
    });
    //
    closeUnpkDiv = function () {
        $('.main_unpk').hide();
        $('#btnUnpkQty').val('');
        $('#btnUnpkReQty').val('');
    }
    cbkcheckkk = function () {
        //合并包裹 选择特效 begin
        $('.chkCheck').click(function () {
            $('#hidMMNum').val($(this).find("input[type='checkbox']").attr('dt-ordernum'));
            $('#hidMMId').val($(this).find("input[type='checkbox']").attr('dt-tid'));
            $('#hidMMQty').val($(this).find("input[type='checkbox']").attr('dt-oqty'));
            //如果已经被选中
            if ($(this).find("input[type='checkbox']").is(':checked')) {
                //如果还没有没选中
                $(this).css('background-color', '#fff');
                $(this).css('color', '#333');
                $(this).find("input[type='checkbox']").prop('checked', false);
            } else {
                //如果还没有没选中
                $(this).css('background-color', '#1aa8f4');
                $(this).css('color', '#fff');
                $(this).find("input[type='checkbox']").prop('checked', 'checked');
            }
            var cnt = 0; //计数器
            $('.cbkWaybill').each(function () {
                if ($(this).is(':checked')) {
                    cnt = cnt + parseInt($(this).val());
                }
            });
            var oqty = 0; // $(this).find("input[type='checkbox']").attr('dt-oqty');
            //重新计算
            $(this).parent().parent().parent().find("input[type='checkbox']").each(function () {
                var disp = ($(this).parent('span').css('display'));
                if (disp != 'none') {//隐藏的不再计算之内部
                    oqty = parseInt($(this).val()) + oqty;
                }
            });
            //重新计算end
            $('#btnMergeQty').val(cnt);
            $('#btnMergeReQty').val(oqty - cnt);
            //行内弹出合并包裹 begin
            var ttmegre = $('#waybill_tb_' + $(this).find("input[type='checkbox']").attr('dt-tid'))
            $('#main_merge').css('top', ttmegre.position().top + 60);
            $('#main_merge').css('right', $(this).find("input[type='checkbox']").position().left + 210);
            $('#main_merge').show();
            $('#main_unpk').hide();
            //行内弹出合并包裹 end

            //设置合并包裹号
            var timestamp = new Date().getTime();
            $('#txtMergeNum').val(timestamp);
        });
        //合并包裹 选择特效 end
    }
    //拆包 end


    $('.showOrdersByMer').click(function () {
        var status = $(this).attr('dt-status');
        var id = $(this).attr('dt-mernum');
        if (status == 0) {
            $('.mt_tr_head_' + id).show();
            $('.mt_tr_' + id).show();
            $(this).attr('dt-status', '1');
            $(this).find('s').text('<?= lang('l_936'); //收起订单 ?>');
        } else {
            $('.mt_tr_head_' + id).hide();
            $('.mt_tr_' + id).hide();
            $('.tb_ship_id_' + id).hide();
            $('.mt_tr_12 .shoow').each(function () {
                $(this).find('s').text('<?= lang('l_566'); //展开来货单?>');
                $(this).attr('data-status', '0');
            });
            $(this).attr('dt-status', '0');
            $(this).find('s').text('<?= lang('l_942'); //展开订单  ?>');
        }

    });
    //设置订单百分比以及添加按钮的显示与隐藏 begin
    setQtyAndAdd = function (id) {
        var pp = $('#waybill_tb_' + id).find('.subtoteltd').last().text().replace('%', '');
        if (pp > 100) {
            $('#per_' + id).css("color", "#db3652");
            $('#per_' + id).css("font-weight", "bold");
        }
        $('#per_' + id).text($('#waybill_tb_' + id).find('.subtoteltd').last().text());
    }
    //设置订单百分比以及添加按钮的显示与隐藏 end

    //合并订单 begin
    $('.merge_orders').click(function () {
        var uri = '<?= blang_url('orders/Multi/ajaxMergePackageByOrders') ?>';
        var mer = $(this).attr('dt-mernum');
        var ods = ''; //订单号集合
        $('.sub_cbk_' + mer).each(function () {
            if ($(this).is(':checked')) {
                ods = $(this).val() + ',' + ods;
            }

        });
        if (ods.length > 0) {
            $.post(uri, {'order_num': ods}, function (dt) {

                if (dt > 0) {
                    layer.msg('<?= lang('l_973'); //恭喜你！修改成功！?>');
                } else {
                    layer.msg("<?= lang('l_975'); //亲，这些订单没有可维修的产品 ?>");
                }

                window.location.href = location.href;
            });
        } else {
            layer.msg('<?= lang('l_981'); //亲，您还没有选择订单！?>');
        }


    });
    //合并订单 end


    /* 显示遮罩层 */
    function showOverlay() {
        $("#overlay").height(pageHeight());
        $("#overlay").width(pageWidth());
        // fadeTo第一个参数为速度，第二个为透明度
        // 多重方式控制透明度，保证兼容性，但也带来修改麻烦的问题
        $("#overlay").fadeTo(200, 0.5);
    }

    /* 隐藏覆盖层 */
    function hideOverlay() {
        layer.closeAll();
        $("#overlay").fadeOut(200);
    }

    /* 当前页面高度 */
    function pageHeight() {
        return document.body.scrollHeight;
    }

    /* 当前页面宽度 */
    function pageWidth() {
        return document.body.scrollWidth;
    }


    if ($('#searchOrderNum').val() != '') {
        $('.showOrdersByMer').trigger("click");
        $('.onum').each(function () {
            $(this).html($(this).text().replace($('#searchOrderNum').val(), '<font style="background-color:#ffff00">' + $('#searchOrderNum').val() + '</font>'));
        });
    }

    //打印来货单 begin
    printWaybill = function (num, onum) {
        $.ajax({
            type: "POST",
            url: "<?= blang_url('orders/Multi/ajaxGetBarcode') ?>",
            saync: false,
            data: {
                'num': num,
                'orderNum': onum
            }, dataType: "text",
            success: function (data) {
                $('#myPrintArea').html($.trim(data));
                $("div#myPrintArea").printArea();
            }

        });
    }
    //打印来货单end

</script>

</div>